package org.carl.nginx.core;

import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.Setting;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NginxConfig {
    /**
     * nginx主目录位置
     */
    private static final String NGINX_HOME="nginx.home";
    /**
     * 需要监听重启的目录
     */
    private static final String SCAN_DIR="scan.dir";

    /**
     * 执行路径
     */
    private static final String EXEC_PATH="exec.path";
    /**
     * 系统前缀
     */
    private static final String EXEC_PREFIX="exec.prefix";
    /**
     * 日志读取
     */
    private static final String TAIL_LOG="tail.log";

    private static final Logger LOG=Logger.getGlobal();
    private static Setting setting=null;
    private static final String FILENAME="nginx.setting";
    private static final String DIR=System.getProperty("user.dir");
    static {
        File file = new File(DIR+File.separator+FILENAME);
        if(!file.exists()){
            LOG.log(Level.FINE, StrUtil.format("{}下未发现{}配置文件,无法启动",DIR,FILENAME));
            System.exit(1);
        }
        setting=new Setting(file.getPath());
    }

    /**获取nginx主目录
     * @return
     */
    public static final String getNginxHome(){
        return setting.getStr(NGINX_HOME);
    }
    public static final String getExecPath(){
        return setting.getStr(EXEC_PATH);
    }
    public static final String getExecPrefix(){
        return setting.getStr(EXEC_PREFIX);
    }
    public static final String getTailLog(){
        String errorLog = setting.getStr(TAIL_LOG);
        if(StrUtil.isEmpty(errorLog)){
            errorLog="logs"+File.separator+"error.log";
        }
        return errorLog;
    }

    /**
     * 获取扫描的目录
     * @return String[]
     */
    public static final String[] getScanDir(){
        String dir = setting.getStr(SCAN_DIR);
        if(StrUtil.isEmpty(dir)){
            return null;
        }
        return StrUtil.split(dir,StrUtil.COMMA);
    }
}
